package net.pinnoocle.playservice.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.exception.ShopException;
import net.pinnoocle.playcommon.response.ErrorCode;
import net.pinnoocle.playcommon.utils.ConvertUtils;
import net.pinnoocle.playcommon.utils.WechatUtils;
import net.pinnoocle.playdao.dao.RegisterWechatDao;
import net.pinnoocle.playdao.dao.ShopDao;
import net.pinnoocle.playdao.entity.RegisterWechat;
import net.pinnoocle.playdao.entity.admin.RegisterWechatEditParams;
import net.pinnoocle.playdao.entity.admin.RegisterWechatSearchParams;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.page.PageQuery;
import net.pinnoocle.playdao.entity.web.wx.FastRegisterApp;
import net.pinnoocle.playdao.entity.wechat.RegisterWechatListParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * 申请小程序(RegisterWechat)表服务实现类
 *
 * @author ye
 * @since 2021-11-17 09:30:19
 */
@Service("registerWechatService")
@Slf4j
public class RegisterWechatServiceImpl extends ServiceImpl<RegisterWechatDao, RegisterWechat> {

    @Autowired
    WxServiceImpl wxService;
    @Autowired
    ShopServiceImpl shopService;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    ShopDao shopDao;

    /**
     * 快速注册小程序
     * @param app
     * @return
     */
    public boolean fastRegisterWechat(FastRegisterApp app){
        try {
            RegisterWechat registerWechat = ConvertUtils.sourceToTarget(app, RegisterWechat.class);
            registerWechat.setCreatedTime(new Date());
            return this.save(registerWechat);
        } catch (Exception e) {
            log.info("申请失败："+e.getMessage());
        }
        return false;
    }

    /**
     * 注册的小程序列表
     * @return
     */
    public PageModel<RegisterWechatListParams> registerWechat(RegisterWechatSearchParams params){
        Map<String,Object> pageMap = new HashMap<>();
        pageMap.put("page",String.valueOf(params.getPage()));
        pageMap.put("pageSize",String.valueOf(params.getPageSize()));

        Page<RegisterWechatListParams> page = shopDao.registerWechat(
                new PageQuery<RegisterWechat>(pageMap).getPage(), params);
        List<RegisterWechatListParams> records = page.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageModel<>(page, new ArrayList<>());
        }
        return new PageModel<>(page, records);
    }

    /**
     * 修改公司信息
     * @param params
     * @return
     */
    public boolean editCompanyInfo(RegisterWechatEditParams params){
        try {
            RegisterWechat registerWechat = ConvertUtils.sourceToTarget(params, RegisterWechat.class);
            return this.updateById(registerWechat);
        } catch (Exception e) {
            log.info("修改公司信息："+e.getMessage());
        }
        return false;
    }

    /**
     * 是否发送给微信审核
     * @param registerId
     * @param sendToWx
     * @return
     */
    public boolean sendToWx(String registerId,Integer sendToWx){
        RegisterWechat registerWechat = this.getById(registerId);
        if(Objects.isNull(registerWechat)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"申请记录不存在");
        }
        //发送给微信
        if(sendToWx == 1){
            String componentAccessToken = wxService.getComponentAccessToken();
            HashMap<String, Object> requestMap = new HashMap<>();
            requestMap.put("name",registerWechat.getCompanyName());
            requestMap.put("code",registerWechat.getCompanyCode());
            requestMap.put("code_type",registerWechat.getCompanyCodeType());
            requestMap.put("legal_persona_wechat",registerWechat.getPersonaWechat());
            requestMap.put("legal_persona_name",registerWechat.getPersonaName());
            requestMap.put("component_phone",registerWechat.getComponentPhone());
            String result = WechatUtils.fastRegisterWechat(requestMap, componentAccessToken);
            if(Objects.equals(result, "success")){
                String shopId = shopService.addShopByRegisterWechat(registerWechat);
                registerWechat.setShopId(shopId);
                this.updateById(registerWechat);
                return true;
            }else{
                log.info("申请微信小程序失败："+result);
                throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
            }
        }else if(sendToWx == 2){
            registerWechat.setSendToWx(2);
            return this.updateById(registerWechat);
        }
        return true;
    }

    /**
     * 重新发起微信审核
     * @param registerId
     * @return
     */
    public boolean againSendToWx(String registerId){
        RegisterWechat registerWechat = this.getById(registerId);
        if(Objects.isNull(registerWechat)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"申请记录不存在");
        }
        String componentAccessToken = wxService.getComponentAccessToken();
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("name",registerWechat.getCompanyName());
        requestMap.put("code",registerWechat.getCompanyCode());
        requestMap.put("code_type",registerWechat.getCompanyCodeType());
        requestMap.put("legal_persona_wechat",registerWechat.getPersonaWechat());
        requestMap.put("legal_persona_name",registerWechat.getPersonaName());
        requestMap.put("component_phone",registerWechat.getComponentPhone());
        String result = WechatUtils.fastRegisterWechat(requestMap, componentAccessToken);
        if(Objects.equals(result, "success")){
            return true;
        }else{
            log.info("申请微信小程序失败："+result);
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,result);
        }
    }
}